package com.qfedu.firstpage.service.impl;

import com.qfedu.firstpage.dao.BbookMapper;
import com.qfedu.firstpage.entity.Bbook;
import com.qfedu.firstpage.entity.Bhistory;
import com.qfedu.firstpage.entity.Blist;
import com.qfedu.firstpage.entity.Blisthistory;
import com.qfedu.firstpage.service.SearchBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author ban
 * @date 2021/3/24 - 17:58
 */
@Service
public class SearchBookServiceImpl implements SearchBookService {

    @Autowired
    private BbookMapper bbookMapper;

    /**
     * 用户 删除 自己的 书单浏览历史
     *
     * @param uid
     * @param lid
     * @return
     */
    @Override
    public boolean deletelistInhistory(int uid, int lid) {
        int num = bbookMapper.deletelistInhistory(uid, lid);
        return num > 0 ? true : false;
    }

    /**
     * 把 用户 浏览过得 书单 加入 到 书单浏览历史中
     *
     * @param uid
     * @param lid
     * @return
     */
    @Override
    public boolean addlistInhistory(int uid, int lid) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        // 浏览 的 时间
        String seeTime = format.format(new Date());

        Blisthistory blisthistory = bbookMapper.selectlistInhistory(uid, lid);

        int num = 0;
        if (null == blisthistory) {
            num = bbookMapper.addlistInhistory(uid, lid, seeTime);
        } else {
            num = bbookMapper.updatelistInhistory(blisthistory.getBlistid(), seeTime);
        }

        return num > 0 ? true : false;
    }

    /**
     * 查询 当前 书单的 所有 书籍 信息
     *
     * @return
     */
    @Override
    public List<Bbook> findblistBylid(int lid) {
        return bbookMapper.findblistBylid(lid);
    }

    /**
     * 查询 当前 用户 所有的 书单列表
     *
     * @param uid
     * @return
     */
    @Override
    public List<Blist> findAllUidBlist(int uid) {
        return bbookMapper.findAllUidBlist(uid);
    }

    /**
     * 删除当前用户  浏览历史中 指定 bid 书籍 的信息
     *
     * @param uid
     * @param bid
     * @return
     */
    @Override
    public boolean deleteUidHistoryBook(int uid, int bid) {

        int num = bbookMapper.deleteUidHistoryBook(uid, bid);
        return num > 0 ? true : false;
    }

    /**
     * 查询 当前 用户的 浏览历史  根据 书籍名 来 模糊查询
     *
     * @param uid
     * @param name
     * @return
     */
    @Override
    public List<Bbook> findbuserHistoryAllBbook(int uid, String name) {
        return bbookMapper.findbuserHistoryAllBbook(uid, name);
    }

    /**
     * 用户 点击 书籍 把书籍 加入 到 浏览历史中
     *
     * @param uid
     * @param bid
     * @return
     */
    @Override
    public boolean insertbbookHistory(int uid, int bid) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        // 浏览 的 时间
        String seeTime = format.format(new Date());

        Bhistory bhistory = bbookMapper.selectbbookHistory(uid, bid);

        int num = 0;
        if (null == bhistory) {
            num = bbookMapper.insertbbookHistory(uid, bid, seeTime);
        } else {
            num = bbookMapper.updatebbookHistory(bhistory.getHid(), seeTime);
        }

        return num > 0 ? true : false;
    }

    /**
     * 删除 指定 用户  指定的书籍
     *
     * @param bids
     * @param uid
     * @return
     */
    @Override
    public boolean deleteBuserBbook(int[] bids, int uid) {

        boolean flag = true;
        for (int bid : bids) {
            int num = bbookMapper.deleteBuserBbook(bid, uid);
            if (num == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    /**
     * 获取 当前用户书架的 所有 信息
     *
     * @param uid
     * @return
     */
    @Override
    public List<Bbook> findBbookrackByUid(int uid) {

        return bbookMapper.findBbookrackByUid(uid);
    }

    /**
     * 用户 添加 书籍 到 书架中
     *
     * @param bid
     * @param uid
     * @return
     */
    @Override
    public boolean insertBbookInBlist(int bid, int uid) {

        return bbookMapper.insertBbookInBlist(bid, uid);
    }

    @Override
    public List<Bbook> findBookLikeBookName(String name, int state) {

        return bbookMapper.findBookLikeBookName(name, state);

    }
}
